from flask import Flask
from flask_restx import Api
from flask_sqlalchemy import SQLAlchemy
from config import config_by_name
from flask_caching import Cache
from flask_cors import CORS
from flask_socketio import SocketIO

db = SQLAlchemy()

cache = Cache()

socketio = SocketIO()

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config_by_name[config_name])

    cache.init_app(app)

    db.init_app(app)
    CORS(app, resources={r"/*": {"origins": "*"}})

    # 初始化 socketio
    socketio.init_app(app, cors_allowed_origins="*")

    from .api.gateio.views import gateio_ns
    from .api.trading.views import trading_ns
    from .api.multi_account.views import multi_account_ns
    api = Api(app, version='1.0', title='API Documentation', description='A simple API', doc='/docs')

    api.add_namespace(gateio_ns)
    api.add_namespace(trading_ns)
    api.add_namespace(multi_account_ns)

    return app
